<?php
namespace Api\Rds;

class Jobs{

    const STATUS_UN_HANDLE = -1; //未执行
    const STATUS_HANDLING = 0; //处理中
    const STATUS_SUCC = 1; //成功
    const STATUS_FAIL = 2; //失败

    /**
     * 提交一次性任务
     * @param string $job_class 需要执行的脚本类名
     * @param string $note  任务说明
     * @param array $params 任务参数
     * @param bool  $force  是否强制执行
     * @return string 任务ID
     * @throws \S\Exception
     */
    public function submit($job_class, array $params = array(), $note = "", $force = false){
        $url = 'job/once/submit';
        $params = implode(" ", $params);
        $command = "php /data1/htdocs/".APP_NAME."/jobs/job.php {$job_class} {$params}";

        $data = array(
            'command' => $command,
            'force'   => $force ? 1 : 0,
            'note'    => $note,
            'appname' => APP_NAME
        );

        $util = new Util();
        $ret = $util->request($url, $data);
        if($ret){
            $job_id = $ret['data']['job_id'];
            return $job_id;
        }else{
            throw new \S\Exception($util->message, $util->code);
        }
    }

    /**
     * 上送任务的执行信息
     * @param $info
     * @return bool
     */
    public function up($info){
        if(empty($_SERVER['x-rid'])){
            return false;
        }

        $url = 'job/run/upinfo';
        $data = array(
            'info' => $info,
            'run_id' => $_SERVER['x-rid']
        );

        try{
            $ret = (new Util())->request($url, $data);
        }catch (\Exception $e){
            $ret = false;
        }

        return $ret ? true : false;
    }

    /**
     * 查询任务执行情况
     * @param string $job_id
     * @return string
     * @throws \S\Exception
     * 返回数据格式
     * array(
     *      'run_id' => 233,
     *      'status' => 1,
     *      'info' => array(
     *          array(
     *              'time' => '2016-01-01 00:00:00',
     *              'msg'  => '任务开始执行'
     *          ),
     *          array(
     *              'time' => '2016-01-01 00:01:00',
     *              'msg'  => '已处理10%'
     *          ),
     *      )
     *  )
     */
    public function query($job_id){
        $url = 'job/once/query';
        $params = array(
            'job_id' => $job_id,
        );

        $util = new Util();
        $ret = $util->request($url, $params);
        if($ret){
            $job_info = $ret['data'];
            $info = $this->getInfo($job_info);
            $ret_info = array(
                'run_id' => $job_info['run_id'],
                'status' => $job_info['status'],
                'info' => $info,
            );
            return $ret_info;
        }else{
            return false;
        }
    }

    /**
     * 获取任务的执行情况
     * @param $job_info
     * @return mixed
     */
    protected function getInfo($job_info){
        switch($job_info['status']){
            case self::STATUS_UN_HANDLE:
                $info[] = array(
                    'time' => date("Y-m-d H:i:s"),
                    'msg'  => "等待处理"
                );
                break;
            case self::STATUS_HANDLING:
                $info[] = array(
                    'time' => $job_info['exec_start_time'],
                    'msg'  => "任务开始",
                );
                if($job_info['exec_info']){
                    $info[] = $job_info['exec_info'];
                }
                $info[] = array(
                    'time' => date("Y-m-d H:i:s"),
                    'msg'  => "任务处理中"
                );
                break;
            case self::STATUS_SUCC:
                $info[] = array(
                    'time' => $job_info['exec_start_time'],
                    'msg'  => "任务开始",
                );
                if($job_info['exec_info']){
                    $info[] = $job_info['exec_info'];
                }
                $info[] = array(
                    'time' => $job_info['exec_end_time'],
                    'msg'  => "任务完成",
                );
                break;
            case self::STATUS_FAIL:
                $info[] = array(
                    'time' => $job_info['exec_start_time'],
                    'msg'  => "任务开始",
                );
                if($job_info['exec_info']){
                    $info[] = $job_info['exec_info'];
                }
                $info[] = array(
                    'time' => $job_info['exec_end_time'],
                    'msg'  => "任务失败\n".$job_info['exec_export'],
                );
                break;
            default:
                $info[] = array(
                    'time' => date("Y-m-d H:i:s"),
                    'msg'  => "任务异常"
                );
                break;
        }

        return $info;
    }
}